package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.Announcement;
import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    @PostMapping
    public Result add(@RequestBody Announcement announcement) {
        announcementService.add(announcement);
        return Result.success();
    }
    
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Announcement announcement = announcementService.findById(id);
        return Result.success(announcement);
    }
    
    @GetMapping("/published")
    public Result findPublished() {
        List<Announcement> announcements = announcementService.findPublished();
        return Result.success(announcements);
    }
    
    @GetMapping
    public Result findAll() {
        List<Announcement> announcements = announcementService.findAll();
        return Result.success(announcements);
    }
    
    @PutMapping
    public Result update(@RequestBody Announcement announcement) {
        announcementService.update(announcement);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        announcementService.delete(id);
        return Result.success();
    }
} 